package com.seo.controller.transaction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seo.pojo.ReturnBody;
import com.seo.resolver.DefiniteException;
import com.seo.service.proxy.recharge.sdk.ProxyRechargeService;
import com.seo.service.user.recharge.impl.RechargeServiceImpl;
import com.seo.service.user.sdk.ConfigService;
import com.seo.utils.CommonUtil;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: RechargeController
 * @author sbl
 * @version 2018.07.28 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/recharge/")
public class RechargeController {

	@Autowired
	private ProxyRechargeService proxyRechargeService;

	@Autowired
	private RechargeServiceImpl rechargeService;
	
	@Autowired
	private ConfigService configService;

	/**
	 * 充值明细
	 * 
	 * @param params  params
	 * @param request request
	 * @return String
	 */
	@RequestMapping(value = "/list")
	public String list(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		String tradeId = CommonUtil.getUUID();
		log.info("[{}]-充值明细[{}]", tradeId, params);
		Integer userId = Jurisdiction.getT("id", Integer.class);
		params.put("userId", userId);
		ReturnBody rb = rechargeService.getRechargeDetailList(tradeId, params);

		if (rb.isSuccess()) {
			request.setAttribute("page", rb.get("page"));
		}
		request.setAttribute("proxy", Jurisdiction.roleCode("proxy"));
		request.setAttribute("result", rb.getResult());
		return "transaction/recharge_list";
	}

	@RequestMapping(value = "/go")
	public String go(HttpServletRequest request) {
		log.info("充值界面");
		Integer parentId = Jurisdiction.getT("parentId", Integer.class);
		String payStr = configService.getConfig("pay_type");
		List<Map<String, Object>> payList = new ArrayList<>();
		if(StringUtils.isNotEmpty(payStr)) {
			String paysArr[] = payStr.split(",");
			for (String pay : paysArr) {
				String payArr[] = pay.split("-");
				Map<String, Object> obj = new HashMap<>();
				obj.put("code", payArr[0]);
				obj.put("name", payArr[1]);
				if(parentId == 0) {
					payList.add(obj);
				}else {
					if("1".equals(payArr[2])) {
						payList.add(obj);
					}
				}
			}
		}
		request.setAttribute("payList", payList);
		return "transaction/recharge_go";
	}

	@RequestMapping(value = "/qrCode")
	public String qrCode() {
		log.info("充值二维码");
		return "transaction/recharge_qrCode";
	}
	
	@RequestMapping(value = "/offlineQrCode")
	public String offlineQrCode() {
		log.info("线下充值二维码");
		return "transaction/offline_recharge_qrCode";
	}

	@RequestMapping("/addRecharge")
	@ResponseBody
	public ReturnBody addRecharge(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		
		Object payModelObj = params.get("payMode");
		if(payModelObj == null) {
			throw new DefiniteException("支付方式不存在，请联系客服进行线下充值");
		}
		String payStr = configService.getConfig("pay_type");
		if(StringUtils.isEmpty(payStr)) {
			throw new DefiniteException("支付方式不存在，请联系客服进行线下充值");
		}
		
		if(!payStr.contains((String)payModelObj)){
			throw new DefiniteException("支付方式不存在，请联系客服进行线下充值");
		}
		
		String tradeId = CommonUtil.getUUID();
		log.info("[{}]-在线充值", tradeId);
		String curDomain = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
		Integer userId = Jurisdiction.getT("id", Integer.class);
		params.put("userId", userId);
		request.setAttribute("curDomain", curDomain);
		return rechargeService.saveRecharge(tradeId, params);
	}

	/**
	 * 代理客户充值明细
	 * 
	 * @param params  params
	 * @param request request
	 * @return String
	 */
	@RequestMapping(value = "child/list")
	public String childList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		ReturnBody rb = ReturnBody.init();
		String tradeId = CommonUtil.getUUID();
		log.info("[{}]-充值明细[{}]", tradeId, params);
		if (!Jurisdiction.roleCode("proxy")) {
			throw new DefiniteException("请先升级为代理");
		}
		Integer userId = Jurisdiction.getT("id", Integer.class);
		params.put("parentId", userId);
		rb = rechargeService.getRechargeDetailList(tradeId, params);

		if (rb.isSuccess()) {
			request.setAttribute("page", rb.get("page"));
		}
		request.setAttribute("proxy", Jurisdiction.roleCode("proxy"));
		request.setAttribute("result", rb.getResult());
		return "transaction/child/recharge_list";
	}

	@RequestMapping(value = "/child/add")
	public String childAdd() {
		log.info("添加充值界面");
		if (!Jurisdiction.roleCode("proxy")) {
			throw new DefiniteException("请先升级为代理");
		}
		return "transaction/child/recharge_add";
	}

	@RequestMapping("/child/addRecharge")
	@ResponseBody
	public ReturnBody childAddRecharge(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		ReturnBody rb = ReturnBody.init();
		if (!Jurisdiction.roleCode("proxy")) {
			return rb.error("error", "权限不足");
		}
		String tradeId = CommonUtil.getUUID();
		log.info("[{}]-保存充值明细", tradeId);
		Integer parentId = Jurisdiction.getT("id", Integer.class);
		params.put("parentId", parentId);
		return rechargeService.saveArtificialRechargeDetail(tradeId, params);
	}

	/**
	 * 代理客户充值明细
	 * 
	 * @param params  params
	 * @param request request
	 * @return String
	 */
	@RequestMapping(value = "proxy/list")
	public String proxyList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		ReturnBody rb = ReturnBody.init();
		String tradeId = CommonUtil.getUUID();
		log.info("[{}]-充值明细[{}]", tradeId, params);
		if (!Jurisdiction.roleCode("proxy")) {
			throw new DefiniteException("请先升级为代理");
		}
		Integer userId = Jurisdiction.getT("id", Integer.class);
		params.put("userId", userId);
		rb.run(proxyRechargeService.getProxyRechargeDetailList(tradeId, params));

		if (rb.isSuccess()) {
			request.setAttribute("page", rb.get("page"));
		}
		request.setAttribute("result", rb.getResult());
		return "transaction/proxy/recharge_list";
	}

	@RequestMapping(value = "/proxy/add")
	public String proxyAdd() {
		log.info("添加充值界面");
		if (!Jurisdiction.roleCode("proxy")) {
			throw new DefiniteException("请先升级为代理");
		}
		return "transaction/proxy/recharge_add";
	}

	@RequestMapping("/proxy/addRecharge")
	@ResponseBody
	public ReturnBody proxyAddRecharge(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		ReturnBody rb = ReturnBody.init();
		if (!Jurisdiction.roleCode("proxy")) {
			return rb.error("error", "权限不足");
		}
		String tradeId = CommonUtil.getUUID();
		log.info("[{}]-保存充值明细", tradeId);
		Integer userId = Jurisdiction.getT("id", Integer.class);
		params.put("userId", userId);
		return proxyRechargeService.saveProxyRechargeDetail(tradeId, params);
	}

}
